package com.inspur.cloud.dao.dataspace;

import com.inspur.cloud.entity.dataspace.UserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserDao extends JpaRepository<UserEntity, Integer>, JpaSpecificationExecutor<UserEntity> {
    UserEntity findUserByName(String name);

    List<UserEntity> findByNameIn(List<String> nameList);

    UserEntity findUserById(int id);


    @Override
    Page<UserEntity> findAll(Specification<UserEntity> specification, Pageable pageable);

    @Query("update UserEntity u set u.failureTimes = 0")
    @Modifying
    void updateUserFailureTimes();

    @Query("update UserEntity u set u.failureTimes = 0 where u.name='admin'")
    @Modifying
    void updateAdminFailureTimes();

    List<UserEntity> findByFailureTimesGreaterThanEqualAndParentId(Integer failureTimes, Integer parentId);

    List<UserEntity> findByFailureTimesGreaterThanEqual(Integer failureTimes);

    List<UserEntity> findByParentId(Integer parentId);

    @Query("select id from UserEntity u where u.parentId=:parentId")
    List<Integer> findIdByParentId(@Param("parentId") Integer parentId);

    List<UserEntity> findByIdIn(List<Integer> idList);
}
